package leetCode.offer32_i;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * 从上到下打印出二叉树的每个节点，同一层的节点按照从左到右的顺序打印。
 */
public class Solution1 implements Solution{

    @Override
    public int[] levelOrder(TreeNode root) {
        if(root==null) return new int[0];
        List<Integer> result = new ArrayList<>();
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (queue.size()>0){
            TreeNode current = queue.poll();
            if(current.left!=null)
                  queue.add(current.left);
            if(current.right!=null)
                queue.add(current.right);
            result.add(current.val);
        }
        // List<Integer> to int[]
        return result.stream().mapToInt(Integer::valueOf).toArray();
    }


}
